package com.caipu.controller.client;

import com.caipu.common.Result;
import com.caipu.dto.UserInfoDTO;
import com.caipu.dto.UserLoginDTO;
import com.caipu.dto.UserRegisterDTO;
import com.caipu.entity.User;
import com.caipu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/client/user")
@CrossOrigin
public class ClientUserController {
    
    @Autowired
    private UserService userService;
    
    /**
     * 用户注册
     */
    @PostMapping("/register")
    public Result<?> register(@RequestBody UserRegisterDTO registerDTO) {
        return Result.success(userService.register(registerDTO));
    }
    
    /**
     * 用户登录
     */
    @PostMapping("/login")
    public Result<UserInfoDTO> login(@RequestBody UserLoginDTO loginDTO) {
        return Result.success(userService.login(loginDTO));
    }
    
    /**
     * 获取用户信息
     */
    @GetMapping("/{id}")
    public Result<User> getUserInfo(@PathVariable Long id) {
        User user = userService.getById(id);
        if (user != null) {
            // 不返回密码
            user.setPassword(null);
        }
        return Result.success(user);
    }
    
    /**
     * 更新用户昵称
     */
    @PutMapping("/nickname")
    public Result<Void> updateNickname(@RequestBody UpdateNicknameRequest request) {
        boolean success = userService.updateNickname(request.getUserId(), request.getNickname());
        if (success) {
            return Result.success();
        } else {
            return Result.error("更新昵称失败");
        }
    }
    
    /**
     * 更新用户密码
     */
    @PutMapping("/password")
    public Result<Void> updatePassword(@RequestBody UpdatePasswordRequest request) {
        boolean success = userService.updatePassword(request.getUserId(), request.getOldPassword(), request.getNewPassword());
        if (success) {
            return Result.success();
        } else {
            return Result.error("密码更新失败，请检查原密码是否正确");
        }
    }
    
    // 内部类定义请求体
    public static class UpdateNicknameRequest {
        private Long userId;
        private String nickname;
        
        public Long getUserId() { return userId; }
        public void setUserId(Long userId) { this.userId = userId; }
        public String getNickname() { return nickname; }
        public void setNickname(String nickname) { this.nickname = nickname; }
    }
    
    public static class UpdatePasswordRequest {
        private Long userId;
        private String oldPassword;
        private String newPassword;
        
        public Long getUserId() { return userId; }
        public void setUserId(Long userId) { this.userId = userId; }
        public String getOldPassword() { return oldPassword; }
        public void setOldPassword(String oldPassword) { this.oldPassword = oldPassword; }
        public String getNewPassword() { return newPassword; }
        public void setNewPassword(String newPassword) { this.newPassword = newPassword; }
    }
}